<?php

class Login extends Controller {

	function Login()
	{
		parent::Controller();	
        $this->load->library('form_validation');   
	}
	
	function index()
	{

        // if is receiving form data, use a different function
        if ($this->input->post('btn_login') == "Login") 
        {

            if ($this->_process_login_form())
            {

                // no errors, login user
                $valid_user = $this->usermodel->check_valid_user($this->input->post('username',TRUE),$this->input->post('password',TRUE));

                if ($valid_user !== FALSE)
                {
                     
                    // register user session
                    $this->usermodel->login_user($valid_user);

                    //redirect user
                    $this->_redirect_after_login();
                    $data["login_response"] = TRUE; 
                    $data["login_msg"] = "Hello ".$valid_user["username"].", welcome back!";

                    
                } else {
                    
                    // Wrong credentials!
                    $data["login_response"] = TRUE;
                    $data["login_msg"] = "Invalid username and/or password. Please try again.";

                }

            } else {

                // not validated
                $data["login_response"] = TRUE;

            }

        }

        //load page description
        $this->load->model('pageheadersmodel'); 
        $data["pagedescription"] = $this->pageheadersmodel->get_header('login');

        //document title
        $data["document_title"] = DOCUMENT_TITLE . "Login";

        $this->load->view('head', $data); 
        $this->load->view('header');   
        $this->load->view('login', $data);   
        $this->load->view('footer');   

	}

    /**
    * Login Form Processing and Validation
    *
    * @access private
    * @param 
    * @return boolean
    */     
        
    private function _process_login_form()
    {
        
        //Rules        
        $config = array(
                           array(
                         'field'   => 'login_username',
                         'label'   => 'Username',
                         'rules'   => 'trim|required|valid_email'
                          ),
                          array(
                         'field'   => 'login_password',
                         'label'   => 'Password',
                         'rules'   => 'trim|required|xss_clean'
                          )
                    );
        

        $this->form_validation->set_rules($config);
        $this->form_validation->set_error_delimiters('<span id="error">', '</span>');
        
        if ($this->form_validation->run($this) == TRUE)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
         
    }

    
    /**
    * Redirection after Session Registers
    *
    * @access private
    * @param 
    * @return boolean
    */ 
    private function _redirect_after_login() {
        
        redirect($this->usermodel->get_referrer(), 'refresh'); 

    }


    public function logout()
    {
        $this->usermodel->logout();
        redirect("/login");
    }

}

?>